/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.admin.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.admin.dto.MaintainTimeDTO;
import org.springblade.modules.admin.dto.MaintainTimeSubmitDTO;
import org.springblade.modules.admin.entity.BuildingContractMaintainTime;
import org.springblade.modules.admin.entity.BuildingMaintainTime;
import org.springblade.modules.admin.vo.BuildingContractMaintainTimeVO;
import org.springblade.modules.admin.mapper.BuildingContractMaintainTimeMapper;
import org.springblade.modules.admin.service.IBuildingContractMaintainTimeService;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 项目保养作业时间段表 服务实现类
 *
 * @author ysq
 * @since 2024-03-11
 */
@Service
public class BuildingContractMaintainTimeServiceImpl extends BaseServiceImpl<BuildingContractMaintainTimeMapper, BuildingContractMaintainTime> implements IBuildingContractMaintainTimeService {

	@Override
	public IPage<BuildingContractMaintainTimeVO> selectBuildingContractMaintainTimePage(IPage<BuildingContractMaintainTimeVO> page, BuildingContractMaintainTimeVO buildingContractMaintainTime) {
		return page.setRecords(baseMapper.selectBuildingContractMaintainTimePage(page, buildingContractMaintainTime));
	}

	@Override
	public List<MaintainTimeSubmitDTO> selectMaintainTimes(Long contractId) {
		List<BuildingContractMaintainTime> list = list(Wrappers.<BuildingContractMaintainTime>lambdaQuery().eq(BuildingContractMaintainTime::getContractId, contractId));
		Map<Integer, List<BuildingContractMaintainTime>> map = list.stream().collect(Collectors.groupingBy(t -> t.getWeek()));

		// 数据封装
		List<MaintainTimeSubmitDTO> data = new ArrayList<>(7);
		map.forEach((k, v) -> {
			MaintainTimeSubmitDTO item = new MaintainTimeSubmitDTO();
			item.setWeek(k);
			int type = v.get(0).getType();
			item.setType(type);
			if (type != 0) {
				List<MaintainTimeDTO> maintainTimeList = new ArrayList<>(5);
				for (BuildingContractMaintainTime buildingMaintainTime : v) {
					MaintainTimeDTO maintainTime = new MaintainTimeDTO();
					maintainTime.setStartTime(buildingMaintainTime.getStartTime());
					maintainTime.setEndTime(buildingMaintainTime.getEndTime());
					maintainTimeList.add(maintainTime);
				}
				item.setMaintainTimeList(maintainTimeList);
			}
			data.add(item);
		});

		return data;
	}


	@Transactional(rollbackFor = Exception.class)
	@Override
	public boolean submit(Long contractId, List<MaintainTimeSubmitDTO> buildingMaintainTimes) {
		long weekCount = buildingMaintainTimes.stream().map(t -> t.getWeek()).distinct().count();
		if (weekCount != buildingMaintainTimes.stream().count()) {
			throw new ServiceException("周数据设置重复");
		}

		List<BuildingContractMaintainTime> list = this.list(Wrappers.<BuildingContractMaintainTime>lambdaQuery().eq(BuildingContractMaintainTime::getContractId, contractId));
		if (Func.isNotEmpty(list)) {
			// 先删除旧数据
			removeByIds(list.stream().map(t -> t.getId()).collect(Collectors.toList()));
		}

		List<BuildingContractMaintainTime> addList = new ArrayList<>(8);
		for (MaintainTimeSubmitDTO item : buildingMaintainTimes) {
			BuildingContractMaintainTime entity = new BuildingContractMaintainTime();
			entity.setContractId(contractId);
			entity.setType(item.getType());
			entity.setWeek(item.getWeek());
			if (item.getType() == 0) {
				addList.add(entity);
				continue;
			}

			List<MaintainTimeDTO> timeList = item.getMaintainTimeList();
			if (timeList.size() > 5) {
				throw new ServiceException("时间段不能超过5个");
			}
			for (MaintainTimeDTO time : timeList) {
				BuildingContractMaintainTime entity2 = new BuildingContractMaintainTime();
				BeanUtils.copyProperties(entity, entity2);
				entity2.setStartTime(time.getStartTime());
				entity2.setEndTime(time.getEndTime());
				addList.add(entity2);
			}
		}

		return saveBatch(addList);
	}


}
